package com.cjw.service.impl.database;

import com.cjw.mapper.database.RoleMapper;
import com.cjw.pojo.dto.database.RoleDTO;
import com.cjw.pojo.entity.database.Role;
import com.cjw.service.database.ResourceService;
import com.cjw.service.database.RoleService;
import com.cjw.util.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service("roleService")
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private ResourceService resourceService;

    @Override
    public List<RoleDTO> queryByUserId(Long userId) {
        List<Role> roles = roleMapper.queryByUserId(userId);
        return roles.stream()
                .map(this::roleToDTO)
                .collect(Collectors.toList());
    }

    private RoleDTO roleToDTO(Role role) {
        RoleDTO roleDTO = BeanUtils.copy(role, RoleDTO.class);
        roleDTO.setResourceDTOList(resourceService.queryByRoleId(role.getId()));
        return roleDTO;
    }
}
